package org.report4service.utils;

import org.report4service.model.Report;

/**
 *  Used to extract the #See org.report4service.model.Report from a persisted string and the verse vice.
 * @author Jmpango
 */
public class ReportParser {

    private String newReportString;
    private String date;
    private int book;
    private int brochures;
    private int returnVists;
    private int minutes;
    private int magazine;
    private int index;
    private Report report = null;

    public ReportParser() {
    }

    /**
     * The record format: 02-2012|1|3|4|5|6
     * where 1:hrs
     *       3:mag
     *       4:bro
     *       5:bks
     *       6:rv
     * @param reportString
     * @return Report
     */
    public Report getReport(String reportString) {
        extract(reportString);
        report = new Report(date, minutes, magazine, brochures, returnVists, book);
        return report;
    }

    public Report getReport(String reportString, int recordId, int maxRecords) {
        if (recordId > maxRecords - 1) {
            return new Report();
        }
        
        extract(reportString);
        report = new Report(date, minutes, magazine, brochures, returnVists, book);
        return report;

    }

    private void extract(String reportString) throws NumberFormatException {
        index = reportString.indexOf("|");
        date = reportString.substring(0, index);
        reportString = reportString.substring(index + 1);
        index = reportString.indexOf("|");
        minutes = Integer.parseInt(reportString.substring(0, index));
        reportString.indexOf("|");
        reportString = reportString.substring(index + 1);
        index = reportString.indexOf("|");
        magazine = Integer.parseInt(reportString.substring(0, index));
        reportString.indexOf("|");
        reportString = reportString.substring(index + 1);
        index = reportString.indexOf("|");
        brochures = Integer.parseInt(reportString.substring(0, index));
        reportString.indexOf("|");
        reportString = reportString.substring(index + 1);
        index = reportString.indexOf("|");
        book = Integer.parseInt(reportString.substring(0, index));
        reportString.indexOf("|");
        reportString = reportString.substring(index + 1);
        returnVists = Integer.parseInt(reportString);
    }

    /**
     * returns a string containing the report details that will be saved in the db.
     * @param report
     * @return 
     */
    public String getReportString(Report report) {
        return report.getDate() + "|" + report.getMinutes() + "|" + report.getMagazines() + "|" + report.getBrochures() + "|" + report.getBook() + "|" + report.getReturnVisits();
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public int getBook() {
        return book;
    }

    public void setBook(int book) {
        this.book = book;
    }

    public int getBrochures() {
        return brochures;
    }

    public void setBrochures(int brochures) {
        this.brochures = brochures;
    }

    public int getHour() {
        return minutes;
    }

    public void setHour(int hour) {
        this.minutes = hour;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public int getMagazine() {
        return magazine;
    }

    public void setMagazine(int magazine) {
        this.magazine = magazine;
    }

    public String getNewReportString() {
        return newReportString;
    }

    public void setNewReportString(String newReportString) {
        this.newReportString = newReportString;
    }

    public Report getReport() {
        return report;
    }

    public void setReport(Report report) {
        this.report = report;
    }

    public int getReturnVists() {
        return returnVists;
    }

    public void setReturnVists(int returnVists) {
        this.returnVists = returnVists;
    }
}
